<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@include file="/pageRc/common/common.jsp" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="msapplication-tap-highlight" content="no">
    <script type="text/javascript" src="<%=commonPath %>/pageRc/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="<%=commonPath %>/pageRc/js/json2.js"></script>
    <link rel="stylesheet" href="<%=commonPath %>/pageRc/layui/css/layui.css" media="all">
    <script type="text/javascript" src="<%=commonPath %>/pageRc/common/utils.js"></script>
    <script type="text/javascript" src="<%=commonPath %>/pageRc/common/msgTool.js"></script>
    <script type="text/javascript" src="<%=commonPath %>/pageRc/layui/layui.js"></script>
    <title>推送消息</title>
    <script type="text/javascript">
        var path = "<%=commonPath%>";
        var msg = new msgTool();
    </script>
</head>
<body onload="onloadTemp();">

<div class="main layui-clear">
    <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief">
            <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding:1.5rem ">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form layui-form-pane">
                        <form method="post" id="formDemo">
                        	<div class="layui-form-item">
                                <label for="messageName" class="layui-form-label">消息名称</label>
                                <div class="layui-input-block">
                                    <input type="text" id="messageName" name="messageName" required lay-verify="required"
                                           autocomplete="off" class="layui-input" value="${message.messageName}"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="templateId" class="layui-form-label">选用模板</label>
                                <div class="layui-input-block">
                                    <select name="templateId" id="templateId" lay-filter="template">
                                        <c:forEach items="${tempList}" var="template">
                                            <option value="${template.templateId}" <c:if test="${message.templateId == template.templateId}"> selected </c:if>>${template.title}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                        	<div class="layui-form-item">
                                <label for="sendTime" class="layui-form-label">发送时间</label>
                                <div class="layui-input-block">
                                    <input type="text" id="sendTime" name="sendTime" required lay-verify="required"
                                           autocomplete="off" class="layui-input" value="${message.sendTime}">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="messageUrl" class="layui-form-label">发送URL</label>
                                <div class="layui-input-block">
                                    <input type="text" id="messageUrl" name="messageUrl"
                                           autocomplete="off" class="layui-input" value="${message.messageUrl}">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="messageType" class="layui-form-label">消息类型</label>
                                <div class="layui-input-block">
                                    <select name="messageType" id="messageType">
                                        <option value="01" <c:if test="${message.messageType == '01'}"> selected </c:if>>周报</option>
                                        <option value="02" <c:if test="${message.messageType == '02'}"> selected </c:if>>日报</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="sendPerson" class="layui-form-label">发送对象</label>
                                <div class="layui-input-block">
                                    <select name="sendPerson" id="sendPerson">
                                        <option value="01" <c:if test="${message.sendPerson == '01'}"> selected </c:if>>所有人</option>
                                        <option value="02" <c:if test="${message.sendPerson == '02'}"> selected </c:if>>团队经理</option>
                                        <option value="03" <c:if test="${message.sendPerson == '03'}"> selected </c:if>>项目经理</option>
                                    </select>
                                </div>
                            </div>
                            
                        </form>
                        <form method="post" id="tempContent">
	                        <c:if test="${messageData != null}">
	                           	<c:forEach items="${template}" var="templateContent">
	                                  <c:forEach items="${messageData}" var="messageContent">
	                                  <c:if test="${templateContent == messageContent.key}">
	                                  	<div class="layui-form-item">
				                            <label for="${templateContent}" class="layui-form-label">${templateContent}</label>
			                                <div class="layui-input-block">
		                                           <input type="text" id="${templateContent}" name="${templateContent}"
		                                           	autocomplete="off" class="layui-input" value="${messageContent.value}" />
			                                </div>
			                            </div>
			                            </c:if>
	                                  </c:forEach>         
								</c:forEach>	
							</c:if>			
							<c:if test="${messageData == null}">
								<c:forEach items="${template}" var="templateContent">
                                  	<div class="layui-form-item">
			                            <label for="${templateContent}" class="layui-form-label">${templateContent}</label>
		                                <div class="layui-input-block">
		                                    <input type="text" id="${templateContent}" name="${templateContent}"
		                                           autocomplete="off" class="layui-input" value="">
		                                </div>
		                            </div>
								</c:forEach>	
							</c:if>          
						</form>
						<div class="layui-form-item">
                         	<input type="hidden" class="layui-input" value="${sign}" id="sign" name="sign"/>
                     		<input type="hidden" class="layui-input" value="${message.messageId}" id="messageId" name="messageId"/>
                     		<input type="hidden" class="layui-input" value="" id="messageData" name="messageData" />
                            <button class="layui-btn" style="margin-left: 200px" lay-filter="formProTeamAdd" lay-submit>
                            	<c:if test="${sign == 1}">
                            		新增
                           		</c:if>
                           		<c:if test="${sign == 2}">
                            		更新
                           		</c:if>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">

	function onloadTemp(){
		var sign = '${sign}';
		if(sign == '2'){
			var messageData = '${messageData}';
			var tempContent = '${tempList}';
			//alert(messageData);
			//alert(tempContent);
			var json = JSON.parse(messageData);
			alert("编辑");
			$.each(json, function(i) {
				var key = i;
				var value = json[i];
			});
		}
	}
	
    layui.use('form', function () {
        var form = layui.form();
        var index = parent.layer.getFrameIndex(window.name); //获取当前窗体索引
        form.on('submit(formProTeamAdd)', function (data) {
        	var formObject1 = {};
        	var formObject2 = {};
            var data1 = $('#formDemo').serializeArray();
            var data2 = $('#tempContent').serializeArray();
            $.each(data1,function(i,item){
            	formObject1[item.name] = item.value;
           	});
            $.each(data2,function(i,item){
            	formObject2[item.name] = item.value;
           	});
           	var formJson1 = JSON.stringify(formObject1);
           	var formJson2 = JSON.stringify(formObject2);
            data.field.messageData = formJson2;
            //alert("data"+JSON.stringify(data.field));
            //alert("JSON"+JSON.stringify(data.field.messageData));
            msg.sendMsg("/page/message/saveMessage.do", data.field, function (data) {
                layer.alert("保存成功！", function () {
                    window.parent.flush();
                    parent.layer.close(index); //执行关闭
                });
            });
            return false;
        });
        // 选用模板
        form.on('select(template)', function(data){
       	  	var templateId = data.value;
       		$.ajax({ 
       		  type: "post", 
       		  url: '<%=commonPath %>/page/message/'+templateId+'/changeTemplate.do', 
       		  dataType: "JSON", 
       		  success: function (data) { 
	    		  $("#tempContent").html("");
	     		  $.each(data.body.tempList,function(key,value){
	         			var str = '<div class="layui-form-item"><label for="'+value+'" class="layui-form-label">'+value+'</label><div class="layui-input-block"><input type="text" id="'+value+'" name="'+value+'"autocomplete="off" class="layui-input" value=""></div></div>';
	  				$("#tempContent").append(str)
	  			  });
       		  }, 
       		  error: function() {
       		      alert("网络异常，请稍后重试");
       		  } 
       		});
       	});
    });
    
</script>
</body>
</html>